# -*- coding: utf-8 -*-

from hlm.node import *


class NodeAndGate(Node):
    """ This node provides a simple and gate checking for all the input ports.

    It is used for halt check, if all the units reports as halt, the final
    result is true.
    """
    def initialize(self, params: ObjectDict) -> None:
        # Give it a type hint
        params.num_of_ports: int
        # Save the port count.
        self.port.and_ins = [self.port_in_direct(bool)
                             for _ in range(params.num_of_ports)]
        # Prepare for the output ports.
        self.port.and_out = self.port_out(bool)

    tasks = ['task_check_all_in']

    def task_check_all_in(self):
        # Loop and check all the ports.
        for and_in in self.port.and_ins:
            if not and_in.get():
                self.port.and_out.put(False)
                return
        # All the input data is True, put out a True.
        self.port.and_out.put(True)